//
//  ScanViewController.swift
//  MyOfO
//  扫码用车界面
//  Created by Yuri on 2018/3/28.
//  Copyright © 2018年 Yuri. All rights reserved.
//

import UIKit
import AVFoundation
import swiftScan
import FTIndicator

class ScanViewController: LBXScanViewController {
    @IBOutlet weak var pannelView: UIView!
    @IBOutlet weak var torchBtn: UIButton!
    
    var isTorchOn = false
    
    @IBAction func torchBtnTap(_ sender: UIButton) {
        
        isTorchOn = !isTorchOn
        
        scanObj?.changeTorch()
        
        if isTorchOn {
            torchBtn.setImage(#imageLiteral(resourceName: "btn_enableTorch_w"), for: .normal)
        } else {
            torchBtn.setImage(#imageLiteral(resourceName: "btn_unenableTorch_w"), for: .normal)
        }
        
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "扫码骑车"

        //修改导航条的风格
        navigationController?.navigationBar.barStyle = .blackTranslucent
        navigationController?.navigationBar.tintColor = UIColor.white
        
        var style = LBXScanViewStyle()
        style.anmiationStyle = .NetGrid
        style.animationImage = UIImage(named: "CodeScan.bundle/qrcode_scan_full_net")
        
        scanStyle = style
        
    }
    
    override func viewDidAppear(_ animated: Bool) {
        guard requestCameraPermission() else {
            return
        }
        super.viewDidAppear(true)
        
        view.bringSubview(toFront: pannelView)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        navigationController?.navigationBar.barStyle = .default
        navigationController?.navigationBar.tintColor = UIColor.black
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    //扫码结果
    override func handleCodeResult(arrayResult: [LBXScanResult]) {
        if let scanResult = arrayResult.first {
            print(scanResult.strScanned)
            
            FTIndicator.setIndicatorStyle(.dark)
            FTIndicator.showToastMessage(scanResult.strScanned)
        }
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */
    
    //请求相机权限
    func requestCameraPermission() -> Bool {
        let author = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
        if author == AVAuthorizationStatus.denied {
            
            let alertController = UIAlertController(title: "提示", message: "相机被禁用，请在设置-隐私中开启", preferredStyle: .alert)
//            alertController.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil))
            alertController.addAction(UIAlertAction(title: "取消", style: .cancel, handler: { (action: UIAlertAction) in
                self.navigationController?.popViewController(animated: true)
            }))
            alertController.addAction(UIAlertAction(title: "设置", style: .destructive, handler: { (action: UIAlertAction) in
                UIApplication.shared.openURL(URL(string: UIApplicationOpenSettingsURLString)!)
            }))
            present(alertController, animated: true, completion: nil)
            return false
        }
        return true
    }

}
